import tempMask from "@/utils/tempMask"
import { requestLogin, requestUserInfo, requestLoginOut } from "@/api"
const state = {
  userTempId: tempMask(),
  token: localStorage.getItem('user_Token') || "",
  nickName: ''
}
const mutations = {
  // 设置token
  setToken(state, payload) {
    state.token = payload
    // 本地存储
    localStorage.setItem("user_Token", payload);
  },
  // 设置用户名
  setUserName(state, payload) {
    state.nickName = payload
  },
  // 退出登录后 清空数据
  signOut(state) {
    state.userTempId = ""
    state.token = ""
    state.nickName = ""
    localStorage.removeItem('user_Token')
  }
}
const actions = {
  // 获取token
  async getToken({ commit }, userInfo) {
    const res = await requestLogin(userInfo)
    commit("setToken", res.token)
  },
  // 获取用户名
  async getUserName({ commit }) {
    const res = await requestUserInfo()
    commit("setUserName", res.nickName)
  },
  // 登出
  async getLoginOut({ commit }) {
    await requestLoginOut()
    commit('signOut')
  }
}
const getters = {
}
export default {
  // 命名空间打开
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}